<template>

    <div>
        <div class="page-header">
            <h1>
                <router-link to="/business/course">
                    {{courseinfo.name}}
                </router-link>
            </h1>
        </div>

        <p>
            <router-link to="/business/course" class="btn btn-white btn-round btn-default">
                <i class="ace-icon fa fa-arrow-left"></i>
                返回课程
            </router-link>
            <button @click="list" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-refresh green"></i>
                刷新
            </button>
            &nbsp;
            <button @click="add" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-edit red"></i>
                新增
            </button>
        </p>
        <pagination ref="pagination" v-bind:list="list"></pagination>
        <table id="simple-table" class="table  table-bordered table-hover">
            <thead>
            <tr>
                <th>ID</th>
                <th>名称</th>
                <th>操作</th>
            </tr>
            </thead>

            <tbody>
            <tr v-for="chapter in chapters">
                <td>{{chapter.id}}</td>
                <td>{{chapter.name}}</td>
                <td>
                    <div class="hidden-sm hidden-xs btn-group">
                        <button @click="toSection(chapter)" class="btn btn-white btn-sm btn-success">
                            <i class="ace-icon fa fa-check bigger-120"></i>小节
                        </button>

                        <button @click="edit(chapter)" class="btn btn-sm btn-white btn-info">
                            <i class="ace-icon fa fa-pencil bigger-120"></i>编辑
                        </button>

                        <button @click="handledelete(chapter.id)" class="btn btn-sm btn-white btn-danger">
                            <i class="ace-icon fa fa-trash-o bigger-120"></i>删除
                        </button>

                    </div>

                    <div class="hidden-md hidden-lg">
                        <div class="inline pos-rel">
                            <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown"
                                    data-position="auto">
                                <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                            </button>

                            <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                                <li>
                                    <a @click="toSection(chapter)" class="tooltip-info" data-rel="tooltip" title="小节">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                                    </a>
                                </li>

                                <li>
                                    <a @click="edit(chapter)" class="tooltip-success" data-rel="tooltip" title="编辑">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                                    </a>
                                </li>

                                <li>
                                    <a @click="handledelete(chapter.id)" class="tooltip-error" data-rel="tooltip" title="删除">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>
        <!-- /.span -->

        <div class="modal fade" id="myModal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">表单</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">名称:</label>
                                <div class="col-sm-10">
                                    <input type="text" v-model="chapterinfo.name" class="form-control" placeholder="名称">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">课程:</label>
                                <div class="col-sm-10">
                                    <p class="form-control-static">{{courseinfo.name}}</p>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" @click="save" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->


    </div><!-- /.row -->


</template>


<script scope>
    import axios from 'axios';
    import {defineComponent, onMounted, ref} from 'vue';
    import Pagination from '@/components/pagination.vue';
    import router from "../../router";

    export default defineComponent({
        name: 'chapter',
        components:{
            Pagination
        },
        setup() {
            const chapters = ref();
            const pagination = ref({});
            const chapterinfo=ref({});
            let courseinfo=ref({});

            const list = () => {
                Loading.show();
                axios.get("/business/admin/chapter/list", {
                    params: {
                        page: pagination.value.page,
                        size: pagination.value.size,
                        courseId: courseinfo.value.id,
                    }
                }).then((response) => {
                    Loading.hide();
                    const data=response.data;
                    if (data.success){
                        chapters.value = data.content.list;
                        pagination.value.render(pagination.value.page,data.content.total);
                    }else {
                        Loading.hide();
                        Toast.warning(data.message);
                    }

                })
            };

            const add=()=>{
                chapterinfo.value={};
                $('#myModal').modal();
            };

            /**
             * 编辑
             */
            const edit = (chapter) => {
                $('#myModal').modal();
                chapterinfo.value = Tool.copy(chapter);
            };

            /**
             * 删除
             */
            const handledelete = (id) => {
                Confirm.show("删除大章后不可恢复，确认删除？", function () {
                    Loading.show();
                    axios.delete("/business/admin/chapter/delete/" + id).then((response) => {
                        Loading.hide();
                        const data = response.data;
                        if (data.success) {
                            list();
                            Toast.success("删除成功!");
                        }else{
                            Loading.hide();
                            Toast.warning(data.message);
                        }
                    })
                });
            };

            const save=()=>{
                // 保存校验
                if (!Validator.require(chapterinfo.value.name, "名称")) {
                    return;
                }
                chapterinfo.value.courseId=courseinfo.value.id;

                Loading.show();
                axios.post("/business/admin/chapter/save",chapterinfo.value).then((response) => {
                    const data=response.data;
                    if (data.success){
                        Loading.hide();
                        $('#myModal').modal('hide');
                        // console.log(data);
                        list();
                        Toast.success("保存成功！");
                    }else{
                        Loading.hide();
                        Toast.warning(data.message);
                    }

                })
            };
            /*
            * 获取从课程界面传值的信息
            * */
            const getCourse=()=>{
                let course=SessionStorage.get(SESSION_KEY_COURSE) || {};
                if(Tool.isEmpty(course)){
                    router.push("/welcome");
                }
                console.log("course",course);
                courseinfo.value=course;
            }

            /**
             * 点击[小节]
             */
            const toSection = (chapter) => {
                //存值
                SessionStorage.set(SESSION_KEY_CHAPTER,chapter);
                //界面跳转
                router.push("/business/section");
            };

            onMounted(() => {
                getCourse();
                list();

            });

            return {
                getCourse,
                list,
                courseinfo,
                add,
                edit,
                handledelete,
                pagination,
                chapterinfo,
                save,
                chapters,
                toSection
            }


        }
    });
</script>

<style scoped>
    .page-header{
        margin: 0 0 12px;
        border-bottom: 1px dotted #E2E2E2;
        padding-bottom: 16px;
        padding-top: 7px;
    }
    .page-header h1 {
        padding: 0;
        margin: 0 8px;
        font-size: 24px;
        font-weight: lighter;
        color: #2679B5;
    }
    .page-header h1 small {
        margin: 0 6px;
        font-size: 14px;
        font-weight: 400;
        color: #8089A0;
    }

</style>